package com.mebs.model.service.accommodation;

import com.mebs.model.service.Service;
import com.mebs.model.CurrencyName;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

import org.jt.jet.util.JetUtils;
import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Mar 14, 2006, 2:50:17 PM
 */
@Entity()
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "ACCOMMODATION")
public class Accommodation extends Service {

    @Column(name = "Employee", nullable = true, length = 250)
    private String employee;

    @ManyToOne(optional = false)
    @JoinColumn(name = "UnitId")
    private AccommodationUnit unit;

    @Column(name = "StartDate", nullable = false)
    private Date startDate;

    @Column(name = "EndDate", nullable = true)
    private Date endDate;

    @Column(name = "PricePerDay", nullable = true)
    private Double pricePerDay;

    @Column(name = "PricePerMonth", nullable = true)
    private Double pricePerMonth;

    @Column(name = "PricePerYear", nullable = true)
    private Double pricePerYear;

    @ManyToOne(optional = true)
    @JoinColumn(name = "CurrencyNameId")
    @Field(type = ArgumentType.INSTANCE)
    private CurrencyName currencyName;

    @Column(name = "Maid", nullable = true)
    private Boolean maid;

    @Column(name = "DSL", nullable = true)
    private Boolean dsl;

    @Column(name = "Other", nullable = true, length = 1024)
    private String other;

    @Column(name = "Security", nullable = true, length = 1024)
    private String security;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "AccommodationId")
    @OrderBy("reportedDate,fixedDate")
    private Set<AccommodationRepair> repairs;

    @ManyToOne(optional = true)
    @JoinColumn(name = "StatusId")
    private AccommodationStatus status;

    /**
     * ********************** Getters-setters ***********************
     */

    public String getEmployee()
    {
        return employee;
    }

    public void setEmployee(String employee)
    {
        this.employee = employee;
    }

    public AccommodationUnit getUnit()
    {
        return unit;
    }

    public void setUnit(AccommodationUnit unit)
    {
        this.unit = unit;
    }

    public Date getStartDate()
    {
        return startDate;
    }

    public void setStartDate(Date startDate)
    {
        this.startDate = startDate;
    }

    public Date getEndDate()
    {
        return endDate;
    }

    public void setEndDate(Date endDate)
    {
        this.endDate = endDate;
    }

    public Double getPricePerDay()
    {
        return pricePerDay;
    }

    public void setPricePerDay(Double pricePerDay)
    {
        this.pricePerDay = pricePerDay;
    }

    public Double getPricePerMonth()
    {
        return pricePerMonth;
    }

    public void setPricePerMonth(Double pricePerMonth)
    {
        this.pricePerMonth = pricePerMonth;
    }

    public Double getPricePerYear()
    {
        return pricePerYear;
    }

    public void setPricePerYear(Double pricePerYear)
    {
        this.pricePerYear = pricePerYear;
    }

    public CurrencyName getCurrencyName()
    {
        return currencyName;
    }

    public void setCurrencyName(CurrencyName currencyName)
    {
        this.currencyName = currencyName;
    }

    public Boolean getMaid()
    {
        return maid;
    }

    public void setMaid(Boolean maid)
    {
        this.maid = maid;
    }

    public Boolean getDsl()
    {
        return dsl;
    }

    public void setDsl(Boolean dsl)
    {
        this.dsl = dsl;
    }

    public String getOther()
    {
        return other;
    }

    public void setOther(String other)
    {
        this.other = other;
    }

    public String getSecurity()
    {
        return security;
    }

    public void setSecurity(String security)
    {
        this.security = security;
    }

    public Set<AccommodationRepair> getRepairs()
    {
        return repairs;
    }

    public void setRepairs(Set<AccommodationRepair> repairs)
    {
        this.repairs = repairs;
    }

    public AccommodationStatus getStatus()
    {
        return status;
    }

    public void setStatus(AccommodationStatus status)
    {
        this.status = status;
    }

    @Transient
    public Type getRequirementType()
    {
        return Type.ACCOMMODATION;
    }

    @Transient
    public boolean isDeletable()
    {
        return JetUtils.isEmpty(this.getRepairs());
    }

    @Override
    public String toString()
    {
        return (this.getSequence() != null) ? this.getSequence().toString() : "";
    }

    @Transient
    public String getSequenceNumber()
    {
        return String.format("ACC-%s-%05d",
            getCompany().getTicker() == null ? "XXX" : getCompany().getTicker(),
            getSequence()
        );
    }
}
